﻿using System;
using System.Collections.Generic;

namespace TC
{
    /// <summary>
    /// 遇到第一个成功，就结束并返回成功
    /// </summary>
    public class Selector : BTNode, IBack, IRepeat
    {
        public List<FlowOutput<FlowNode>> flows;
        private int cur = 0;

        public override void Execute(Dispatcher chain)
        {
            base.Execute(chain);

            if (cur < flows.Count)
            {
                chain.Call(flows[cur++]);
            }
            else
            {
                //如果到最后都没有选出来，表示返回失败
                chain.End(1);
            }
        }

        public override void Stop()
        {
            base.Stop();

            cur = 0;
        }

        public void Next(int status)
        {
            if (status == 0)
            {
                chain.End(status);
            }
            else
            {
                Execute(chain);
            }
        }

        public void Back()
        {
            if (cur <= 0) throw new Exception("Selector back cur <= 0");

            --cur;
        }
    }
}
